package com.anbang.website.listener;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import com.anbang.website.utils.Constant;

/**
 * Application Lifecycle Listener implementation class InitSetupListener
 *
 */
@WebListener
public class InitSetupListener implements ServletContextListener {

    /**
     * Default constructor. 
     */
    public InitSetupListener() {
        
    }

	/**
     * @see ServletContextListener#contextInitialized(ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent event)  { 
    	String rootPath = event.getServletContext().getRealPath("/WEB-INF/classes");

		File f = new File(rootPath + "/website.properties");
		if (f.exists()) {
			try {
				InputStream is = new FileInputStream(f);
				Constant.webconfig.clear();
				Constant.webconfig.load(is);
				is.close();
				event.getServletContext().log(rootPath + "/website.properties already loaded.");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
    }

	/**
     * @see ServletContextListener#contextDestroyed(ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent event)  { 
    }
	
}
